package com.sm.service;

import com.sm.bo.HappyOrderBo;
import com.sm.dao.HappyOrderMapper;
import com.sm.dao.HappyRequestMapper;
import com.sm.model.HappyOrder;
import com.sm.model.HappyRequest;
import com.sm.util.CommonUtil;
import com.sm.wx.vo.AjaxResult;
import com.sm.wx.vo.WxOrderVO;
import com.sm.wx.wxpay.WxPay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@Service
public class HappyOrderService {

    @Autowired
    private HappyOrderMapper happyOrderMapper;
    @Autowired
    private WxPay wxPay;

    @Autowired
    private HappyRequestMapper happyRequestMapper;


    // 创建订单
    @Transactional
    public AjaxResult createOrder(HttpServletRequest request, HappyOrderBo happyOrderBo){
        //该订单的订单号:
        String orderNum = "XF"+CommonUtil.getOutTradeNo();
        HappyOrder happyOrder = new HappyOrder();
        happyOrder.setMoney(happyOrderBo.getPrice().doubleValue());
        happyOrder.setUserId(happyOrderBo.getUserId());
        happyOrder.setOrderNum(orderNum);
        happyOrder.setOrderTime(new Date());
        happyOrder.setPayType(happyOrderBo.getPayType());
        happyOrder.setStatus(-2);
        happyOrder.setIsRefund(-1);
        happyOrderMapper.insertSelective(happyOrder);
        if (happyOrderBo.getPayType() == 2){
            WxOrderVO wxOrderVO = new WxOrderVO("幸福问答订单", "1", orderNum, happyOrderBo.getPrice().multiply(new BigDecimal("100")).intValue()+"", "happyNotify.do");
            return wxPay.appPay(request, wxOrderVO);
        }else {
            return new AjaxResult().addError("支付失败");
        }
    }

    /**
     * 获取订单是否支付完成
     * @param orderNum
     * @return  false代表未完成需要重新回调 true代表完成不需要回调
     */
    public boolean getIsFinish(String orderNum){
        HappyOrder order = happyOrderMapper.selectByOrderNum(orderNum);
        return !order.getStatus().equals(-2);
    }

    //更改订单状态
    public void updateHappyOrder(String orderNum){

        HappyOrder byOrderNum = happyOrderMapper.selectByOrderNum(orderNum);
        HappyRequest happyRequest = new HappyRequest();
        happyRequest.setCreateTime(new Date());
        happyRequest.setRequestUser(byOrderNum.getUserId());
        happyRequest.setTitle(byOrderNum.getTitle());
        happyRequest.setContent(byOrderNum.getContent());
        happyRequestMapper.insertRequest(happyRequest);
        log.info("支付完成添加问题成功");
        HappyOrder happyOrder = new HappyOrder();
        happyOrder.setRequestId(happyRequest.getRequestId());
        happyOrder.setFinishTime(new Date());
        happyOrder.setStatus(0);
        happyOrder.setPayTime(new Date());
        happyOrder.setOrderNum(orderNum);
        happyOrderMapper.updateByOrderNum(happyOrder);
        log.info("幸福问答订单完成，修改成功状态");
    }

    //根据orderNum查订单
    public HappyOrder getByOrderNum(String orderNum){
        return happyOrderMapper.selectByOrderNum(orderNum);
    }






}

